﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace LohnefinkTower
{
    class Animation
    {
        Texture2D texture;
        float scale;
        int elapsedTime;
        int frameTime;
        int frameCount;
        int currentFrame;
        Rectangle sourceRect;
        Rectangle destinationRect;
        int frameWidth;
        int frameHeight;
        bool active;
        bool looping;
        public Vector2 position;

        public Animation (Texture2D texture, float scale, int frameTime, int frameCount, int frameWidth, int frameHeight, bool looping, Vector2 position)
        {
            this.texture = texture;
            this.scale = scale;
            this.frameTime = frameTime;
            this.frameCount = frameCount;
            this.frameWidth = frameWidth;
            this.frameHeight = frameHeight;
            this.looping = looping;
            this.position = position;

            elapsedTime = 0;
            currentFrame = 0;

            active = true;
        }

        public void Update (GameTime time)
        {
            if (!active)
                return;

            elapsedTime += (int)time.ElapsedGameTime.TotalMilliseconds;

            if (elapsedTime > frameTime)
            {
                currentFrame++;
                if (currentFrame == frameCount)
                    currentFrame = 0;
                if (!looping)
                    active = false;
                elapsedTime = 0;
            }

            sourceRect = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
            destinationRect = new Rectangle((int)position.X, (int)position.Y, (int)(frameWidth * scale), (int)(frameHeight * scale));
        }

        public void draw (SpriteBatch spriteBatch)
        {
            if (active)
                spriteBatch.Draw(texture, destinationRect, sourceRect, Color.White);
        }
    }
}
